import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:pornhub_app/assets/styles.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/utils/ad_jump.dart';
import 'package:visibility_detector/visibility_detector.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../../components/app_bar/extend_transparent_app_bar.dart';
import '../../../../components/base_page/base_error_widget.dart';
import '../../../../components/no_more/no_data.dart';
import '../../../../components/no_more/no_more.dart';
import '../../../../model/advertisements/ad_resp_model.dart';
import '../../../../utils/color.dart';
import '../../../player/views/av_player_loading.dart';
import '../common/short_v_p_cell.dart';
import '../common/short_video_model.dart';
import '../controllers/short_video_player_page_controller.dart';
import 'short_video_hots.dart';
import 'package:pornhub_app/utils/extension.dart';

class ShortVideoPlayerPage extends StatelessWidget {
  final ShortVideoPlayerMode mode;

  const ShortVideoPlayerPage({super.key, required this.mode});

  Widget _buildHotBox(ShortVideoPlayerPageController controller) {
    if (controller.pagingController.itemList?.isEmpty == true) {
      return const SizedBox.shrink();
    }
    return const ShortVideoHots();
  }

  Widget _buildMainArea(ShortVideoPlayerPageController controller) {
    final isLocal = mode == ShortVideoPlayerMode.local;
    return RefreshIndicator(
      onRefresh: () => Future.sync(
        () => controller.pagingController.refresh(),
      ),
      notificationPredicate:
          !isLocal ? defaultScrollNotificationPredicate : (_) => false,
      child: PagedPageView<int, ShortVideoModel>(
        pageController: controller.pageController,
        pagingController: controller.pagingController,
        scrollDirection: Axis.vertical,
        builderDelegate: PagedChildBuilderDelegate(
          itemBuilder: (context, item, index) {
            return ShortVPCell(
              item,
              controller: controller.findCellController(index),
            );
          },
          firstPageProgressIndicatorBuilder: (context) =>
              const AvPlayerLoading(),
          newPageProgressIndicatorBuilder: (context) => const AvPlayerLoading(),
          noItemsFoundIndicatorBuilder: (context) => const NoData(),
          noMoreItemsIndicatorBuilder: (context) => const NoMore(),
          firstPageErrorIndicatorBuilder: (context) => BaseErrorWidget(
            onTap: controller.pagingController.refresh,
          ),
          newPageErrorIndicatorBuilder: (context) => BaseErrorWidget(
            onTap: controller.pagingController.retryLastFailedRequest,
          ),
        ),
        onPageChanged: controller.onPageIndexChanged,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final isLocal = mode == ShortVideoPlayerMode.local;
    final vcTag = mode.index.toString();
    ShortVideoPlayerPageController controller =
        Get.find<ShortVideoPlayerPageController>(tag: vcTag);
    return GetBuilder<ShortVideoPlayerPageController>(
      init: controller,
      tag: vcTag,
      autoRemove: false,
      builder: (controller) => VisibilityDetector(
        key: Key('short-video-pge-${mode.index}'),
        onVisibilityChanged: (info) {
          if (info.visibleFraction == 1) {
            controller.onVisibleChange(true);
          } else if (info.visibleFraction == 0) {
            // 可能已经卸载了
            if (Get.isRegistered<ShortVideoPlayerPageController>(tag: vcTag) ||
                Get.isPrepared<ShortVideoPlayerPageController>(tag: vcTag)) {
              controller.onVisibleChange(false);
            }
          }
        },
        child: Scaffold(
          backgroundColor: Colors.black,
          extendBodyBehindAppBar: isLocal,
          appBar:
              isLocal ? ExtendTransparentAppBar(backColor: COLOR.white) : null,
          body: _buildMainArea(controller),
        ),
      ),
    );
  }
}
